package org.itsolutions.javaBackup.gui.web.common.components;

import static java.util.concurrent.TimeUnit.SECONDS;

import org.itsolutions.javaBackup.gui.web.common.ApplicationDataHolder;

import com.google.inject.Inject;
import com.vaadin.ui.Window.Notification;

/**
 * 
 * @author Tomas Fecko
 */
public final class JBNotification extends Notification {

	private static final long 		serialVersionUID = 1768249194168087463L;
	@Inject ApplicationDataHolder	mAppHolder;
	private boolean					mIsCaptionLocalized = true;
	private boolean					mIsDescriptionLocalized = true;

	/**
	 * Default constructor.
	 */
	@Inject JBNotification() {
		super(null, null, TYPE_HUMANIZED_MESSAGE, true);
		setType(TYPE_HUMANIZED_MESSAGE);
	}
	
	/**
	 * 
	 * @param pApplicationDataHolder application data holder
	 * @param pCaption caption
	 * @param pDescribtion describtion
	 * @param pType type
	 * @param pHtmlContentAllowed htmlContent allowed
	 */
	public JBNotification(ApplicationDataHolder pApplicationDataHolder, String pCaption, String pDescribtion, int pType, 
			boolean pHtmlContentAllowed) {
		super(pCaption, pDescribtion, pType, pHtmlContentAllowed);
		mAppHolder = pApplicationDataHolder;
	}

	@Override
	public String toString() {
		String str = super.toString();
		if (mIsCaptionLocalized && str != null && !str.isEmpty() && mAppHolder != null) {		
			return mAppHolder.getString(str);
		}
		return str;
	}

	@Override
	public String getDescription() {
		String desc = super.getDescription();
		if (mIsDescriptionLocalized && desc != null && !desc.isEmpty() && mAppHolder != null) {
			return mAppHolder.getString(desc);
		}
		return desc;
	}

	@Override
	public String getCaption() {
		String caption = super.getCaption();
		if (mIsCaptionLocalized && caption != null && !caption.isEmpty() && mAppHolder != null) {
			return mAppHolder.getString(caption);
		}
		return caption;
	}

	/**
	 * Caption set by this method will not be localized.
	 * @param pCaption unlocalized caption
	 * @return this
	 */
	public JBNotification setCaptionUnlocalized(String pCaption) {
		super.setCaption(pCaption);
		mIsCaptionLocalized = false;
		return this;
	}

	/**
	 * Description set by this method will not be localized.
	 * @param pDescription description
	 * @return this
	 */
	public JBNotification setDescriptionUnlocalized(String pDescription) {
		super.setDescription(pDescription);
		mIsDescriptionLocalized = false;
		return this;
	}
	
	/**
	 * 
	 * @param pType notification type (e.g. Notification.TYPE_ERROR_MESSAGE)
	 */
    public void setType(int pType) {
        switch (pType) {
        case TYPE_WARNING_MESSAGE:
        	super.setDelayMsec((int) SECONDS.toMillis(2));
        	super.setStyleName("warning");
            break;
        case TYPE_ERROR_MESSAGE:
        	super.setDelayMsec(-1);
        	super.setStyleName("error");
            break;
        case TYPE_TRAY_NOTIFICATION:
        	super.setDelayMsec((int) SECONDS.toMillis(3));
        	super.setStyleName("tray");
        	super.setPosition(POSITION_BOTTOM_RIGHT);
        case TYPE_HUMANIZED_MESSAGE:
        default:
        	super.setDelayMsec((int) SECONDS.toMillis(1));
            break;
        }
    }
}





